<template>
  <div >
    <form-head @onSubmit="onSubmit" :is_change="is_change"/>
    <el-form ref="form" :rules="rules" :model="form" label-width="100px" class="layout-all form-width">
      <el-form-item label="广告类名:" prop="title">
        <el-input v-model="form.title" placeholder="请输入广告名" />
      </el-form-item>
      <el-form-item label="排序:" prop="sort">
        <el-input v-model="form.sort" placeholder="请输入排序" type="number" class="form-width-n"/>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name:'index_ui_form',
  data() {
    return {
      id: 0,
      is_change: undefined,
      options: [],
      form: {
        id: 0,
        sort: 0,
        shop_id: 1,
        title: ''
      },
      rules: {
        title: [
          { required: true, message: '请输入广告名', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    this.id = (this.$route.params && this.$route.params.id)||0
    this.$setTagsViewTitle(this.id)
    this.init_into()
  },
  methods: {
    // 提交
    onSubmit(formName) {
      const form = this.form
      console.log(form)
      this.$refs[formName].validate(valid => {
        if (valid) {
          form.id = this.id
          this.get('/admin/AdCate/save', form, 'POST').then(e => {this.$back()})
          
        } else {
          console.log('error submit!!')
          return false
        }
      })
      console.log('submit!')
    },
    // 返回
    bk() {
      this.$router.back()
    },
    // 获取当前数据
    init_into() {
      this.get('/admin/AdCate/info?id=' + this.id, 'POST').then(e => {
        const tpl = e.info.tpl
        this.form.id = tpl.id
        tpl.sort ? this.form.sort = tpl.sort : ''
        this.form.title = tpl.title
        this.form.shop_id = e.shop_id
        setTimeout(()=>{this.is_change=false},100)
      })
    }
  },
  watch: {
    form:{
      handler (val, oldVal) {
	      if(this.is_change!=undefined)
        	this.is_change=true
      },
      deep:true
    }
  }
}
</script>

<style>
.layout-all {
  margin: 10px;
}
</style>
